<?php

App::uses('AppController', 'Controller');

/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {

    public $uses = array('User', 'Migralmacen', 'Group');
    public $layout = 'hospital';

    public function beforeFilter() {
        parent::beforeFilter();
        //$this->checksession();

        //$this->Auth->allow();
    }

    public function login2() {
        if ($this->Session->read('Auth.User')) {
            $this->Session->setFlash('You are logged in!');
            $this->redirect('/', null, false);
        }
    }

    public function login() {
        //debug($this->data);
        $this->layout = 'login';
        if ($this->request->is('post')) {
            //debug($this->request->data);exit;

            if ($this->Auth->login()) {
                $perfil = $this->Session->read("Auth.User.group_id");
                if ($perfil == 1) {
                    $this->redirect(array('controller' => 'Pacientes', 'action' => 'principal'));
                } elseif ($perfil == 3) {
                    $this->redirect(array('controller' => 'Pacientes', 'action' => 'buscarpaciente'));
                } elseif ($perfil == 5) {
                    $this->redirect(array('controller' => 'Consultasexternas', 'action' => 'mispacientes'));
                } else {
                    $this->redirect(array('controller' => 'Consultasexternas', 'action' => 'listadopacientes'));
                }
            } else {
                $this->Session->setFlash('Usuario o Password incorrectos.');
                $url = array('action' => 'login');
                $this->redirect($url);
            }
        }
    }

    public function index() {
        $this->User->recursive = 0;                                                
        $this->set('users', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {

        if ($this->request->is('post')) {
            //debug($this->request->data);exit;
            $pass1 = $this->request->data['User']['password'];
            $pass2 = $this->request->data['User']['password'];

            if ($pass1 == $pass2) {
                $this->User->create();
                if ($this->User->save($this->request->data)) {
                    $this->Session->setFlash(__('The user has been saved'));                    
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
                }
            } else {
                $this->Session->setFlash(__('password incorect, please try again '));
            }
        }
        $groups = $this->User->Group->find('list', array('fields' => 'Group.name'));
        $this->set(compact('groups'));
    }

    public function salir() {
        //$this->Session->setFlash('Hasta luego');
        $this->redirect($this->Auth->logout());
    }

    public function salemoso() {
        //$this->redirect($this->Auth->logout());
        $this->redirect(array('action' => 'verificamoso'));
    }

    //esta funcion ejecuta llenado de aros_acos
    public function initDB() {
        $group = $this->User->Group;
        //Allow admins to everything
        $group->id = 1;
        $this->Acl->allow($group, 'controllers');

        //allow managers to posts and widgets
        /* $group->id = 2;
          $this->Acl->deny($group, 'controllers');
          $this->Acl->allow($group, 'controllers/Posts');
          $this->Acl->allow($group, 'controllers/Widgets');

          //allow users to only add and edit on posts and widgets
          $group->id = 3;
          $this->Acl->deny($group, 'controllers');
          $this->Acl->allow($group, 'controllers/Posts/add');
          $this->Acl->allow($group, 'controllers/Posts/edit');
          $this->Acl->allow($group, 'controllers/Widgets/add');
          $this->Acl->allow($group, 'controllers/Widgets/edit'); */
        //we add an exit to avoid an ugly "missing views" error message
        echo "all done";
        exit;
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id) {
        $this->User->id = $id;
        //debug($id);exit();
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            //debug($this->request->data);exit;
            $this->User->id = $id;
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('El usuario se edito correctamente'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Error. Intentelo de nuevo'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
        }
        $groups = $this->User->Group->find('list', array('fields' => 'Group.name'));
        $this->set(compact('groups'));
        //debug($groups);exit;
    }

    public function editarperfil($id = null) {
        //debug($id);exit();
        //debug($this->request->data);exit;
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            //  debug($this->request->data);
            exit;
            $this->User->id = $id;
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('El usuario se edito correctamente'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Error. Intentelo de nuevo'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
        }
        $groups = $this->User->Group->find('list', array('fields' => 'Group.name'));
        $this->set(compact('groups'));
        //debug($groups);exit;
    }

    /**
     * delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        //debug($this->reque->data);exit;
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Usuario invalido'));
        }
        if ($this->User->delete()) {
            $this->Session->setFlash(__('Usuario eliminado'), 'msgbueno');
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('El usuario no se elemino'), 'msgalert');
        $this->redirect(array('action' => 'index'));
    }

    public function cambiopass($id) {
        $this->User->id = $id;
        if (!$id) {
            $this->Session->setFlash('No existe tal registro');
            $this->redirect(array('action' => 'index'), null, true);
        }
        if (empty($this->data)) {
            $this->data = $this->User->read();
        } else {
            if ($this->User->save($this->data)) {
                $this->Session->setFlash('Su Password fue modificado Exitosamente');
                $this->redirect(array('action' => 'index'), null, true);
            } else {
                $this->Session->setFlash('no se pudo modificar!!');
            }
        }
    }

//    public function editarmipassword($id) {
//        $this->User->id = $id;
//        if (!$id) {
//            $this->Session->setFlash('No existe tal registro');
//            $this->redirect(array('action' => 'index'), null, true);
//        }
//        if (empty($this->data)) {
//            $this->data = $this->User->read();
//        } else {
//            if ($this->User->save($this->data)) {
//                $this->Session->setFlash('Su Password fue modificado Exitosamente');
//                $this->redirect(array('action' => 'index'), null, true);
//            } else {
//                $this->Session->setFlash('no se pudo modificar!!');
//            }
//        }
//    }

    public function verificamoso() {
        $this->layout = 'mosos';
        //$this->request->data['User']['username']='moso';
        if ($this->request->is("post")) {
            //debug($this->request->data);
            //exit;
            $codigo = $this->request->data['User']['codigo'];
            $verificaMoso = $this->User->find('count', array(
                'conditions' => array('User.codigo' => $codigo)
            ));

            $datosMoso = $this->User->find('first', array(
                'conditions' => array('User.codigo' => $codigo)
            ));

            $idMoso = $datosMoso['User']['id'];

            //debug($datosMoso);exit;            
            if ($verificaMoso == 1) {
                $this->redirect(array('controller' => 'Pedidos', 'action' => 'mesas', $idMoso));
                //debug($verificaMoso);exit;
            } else {
                $this->Session->setFlash('Usuario o Password incorrectos.');
                $this->redirect(array('controller' => 'Users', 'action' => 'verificamoso'));
            }
        } else {
            
        }

        /* if ($this->request->is('post'))
          {
          //debug($this->request->data);exit;
          if ($this->Auth->login())
          {
          //$this->redirect($this->Auth->redirect());
          $this->redirect(array('controller' => 'Pedidos', 'action' => 'mesas'));
          }
          else
          {
          $this->Session->setFlash('Usuario o Password incorrectos.');
          }
          }/
          /*if(!empty($this->request->data))
          {
          debug($this->request->data);exit;
          }else{

          } */
    }

}
